package course.c4.controller;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import course.c4.db.derby.entity.Company;
import course.c4.db.derby.repository.CompanyRepository;

/**
 * 第四课 简单的restfull风格的Spring MVC工程.
 * @author wangd
 *
 */
@Controller
@RequestMapping(value = "/c4")
public class C4Controller {
	
	/**
	 * 日志操作对象.
	 */
	private Logger logger = LoggerFactory.getLogger(C4Controller.class);

	@Autowired
	protected CompanyRepository repo;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/courseInfo", method = { RequestMethod.GET } ) //仅支持GET方法
	@ResponseBody() //返回值是JSON类型
	public ResponseEntity<Map<String, Object>> courseInfo(){
		logger.info("C4Controller::couserInfo()方法开始执行...");
		Map<String, Object> result = new HashMap<>();
		result.put("info", "第四课");
		logger.info("C4Controller::courseInfo()方法已经完成。");
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/jpa", method = { RequestMethod.GET } ) //仅支持GET方法
	@ResponseBody() //返回值是JSON类型
	@Transactional
	public ResponseEntity<Map<String, Object>> jpa(){
		Map<String, Object> result = new HashMap<>();
		
		Company c0 = new Company();
		c0.setName("c1");
		c0.setCode("c1");
		c0.setCity("北京");
		
		repo.saveAndFlush(c0);
		
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	
}
